﻿// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they bagin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
// 
// - Sun Tsu,
// "The Art of War"

using System;
using System.Data;
using System.Data.SqlClient;
using log4net.Core;

namespace Infrastructure.Logging.Appenders
{
    /// <summary>
    /// 
    /// </summary>
    public class ManagementStudioErrorDataBaseAppender : DatabaseAppenderBase
    {
        /// <summary>
        /// Prepares the database command and initialize the parameters.
        /// </summary>
        protected override SqlCommand GetCommand(LoggingEvent logEvent)
        {
            SqlCommand command = null;
            if (_connection != null)
            {
                try
                {
                    command = _connection.CreateCommand();
                    command.CommandText = _storedProcedure;
                    command.CommandType = CommandType.StoredProcedure;
                    
                    var username = logEvent.Properties["UserName"];
                    var sessionId = logEvent.Properties["SessionID"];
                    var componentId = GlobalContext.ComponentId;
                    var serverId = GlobalContext.ServiceId;

                    command.Parameters.AddWithValue( "@UtcTime", logEvent.TimeStamp.ToUniversalTime() );
                    command.Parameters.AddWithValue( "@ServerId", serverId.HasValue ? (object)serverId.Value : DBNull.Value );
                    command.Parameters.AddWithValue( "@ServerIp", _serverIp );
                    command.Parameters.AddWithValue( "@ComponentId", componentId.HasValue ? (object)componentId.Value : DBNull.Value );
                    command.Parameters.AddWithValue("@Username", username ?? DBNull.Value);
                    command.Parameters.AddWithValue("@SessionId", sessionId ?? DBNull.Value);
                    command.Parameters.AddWithValue("@Level", logEvent.Level.Name);
                    command.Parameters.AddWithValue( "@Message", logEvent.MessageObject.ToString() );
                    command.Parameters.AddWithValue( "@ExceptionType", logEvent.ExceptionObject == null ? (object)DBNull.Value : logEvent.ExceptionObject.GetType().ToString() );
                    command.Parameters.AddWithValue( "@ExceptionMessage", logEvent.ExceptionObject == null ? (object)DBNull.Value : logEvent.ExceptionObject.Message );
                    command.Parameters.AddWithValue( "@ExceptionFull", logEvent.ExceptionObject == null ? (object)DBNull.Value : logEvent.ExceptionObject.ToString() );
                }
                catch (Exception e)
                {
                    ErrorHandler.Error("Could not create database command [" + _storedProcedure + "]", e);

                    if (command != null)
                    {
                        try
                        {
                            command.Dispose();
                        }
                        catch
                        { }
                    }
                }
            }

            return command;
        }
    }
}
